home *** CD-ROM | disk | FTP | other *** search
- /* Functions for doing things to controls. At the moment it's not
- very fancy, but features will be added as needed.
-
- Revision History:
-
- 93/11/17 aih
- - added function to invalidate control
-
- 93/11/03 aih
- - updated validation function
-
- 93/10/18 aih
- - added functions for creating and initializing a control
-
- 92/02/20 AIH
- - Added pattern parameter to CtlDefaultFrame
-
- 91/06/13 AIH
- - Time library is used to delay so that background tasks get time
-
- 91/05/31 AIH
- - Added a comment explaining why a region is calculated for the default
- button's outline.
-
- 91/04/17 AIH
- - Modified function for framing the default button and added a function
- to erase the frame
-
- 91/04/10 AIH
- - Added functions for accessing elements of a control handle
-
- 91/04/07 AIH
- - Enhanced control validation function
-
- 91/03/11 AIH
- - Added constant for width of a scroll bar
-
- 91/03/03-05 AIH
- - Added constants for my button CDEF
- - A control is only reported as disabled if its hilite is CTL_DISABLED
-
- 91/01/22 AIH
- - Added a couple of functions
-
- 91/01/21 AIH
- - Added comment describing purpose of this file
-
- 91/01/05 Ari Halberstadt (AIH)
- - Inserted this standard header in all files */
-
- #include <string.h>
- #include "pstr.h"
- #include "ControlLib.h"
- #include "DrawLib.h"
- #include "MemoryLib.h"
- #include "RectangleLib.h"
- #include "StringLib.h"
- #include "TimeLib.h"
- #include "WindowLib.h"
-
- /* Hilite values for enabled/disabled control. To check for a disabled or
- enabled control, compare the control's hilite value to CTL_DISABLED.
- Don't compare it to CTL_ENABLED since the control could be enabled
- with a part code, such as inButton for a standard push button. Better
- yet, just use the appropriate functions from this library. */
- #define CTL_ENABLED (0)
- #define CTL_DISABLED (255)
-
- /* true if control is a valid control */
- Boolean CtlValid(ControlHandle ctl)
- {
- ControlHandle next = NULL;
- Rect contrlRect;
-
- if (! HandleValidSize(ctl, sizeof(ControlRecord)-sizeof(Str255)-1)) return(false);
- if (! WinValid((**ctl).contrlOwner)) return(false);
- contrlRect = (**ctl).contrlRect;
- if (! RectValid(&contrlRect)) return(false);
- /* the control must be in its owner's window list */
- next = ((WindowPeek) (**ctl).contrlOwner)->controlList;
- while (next && next != ctl)
- next = (**next).nextControl;
- if (! next) return(false);
- return(true);
- }
-
- /* return the window containing the control */
- WindowPtr CtlWindow(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return((**ctl).contrlOwner);
- }
-
- /* return the control's rectangle */
- void CtlRect(ControlHandle ctl, Rect *contrlRect)
- {
- require(CtlValid(ctl));
- *contrlRect = (**ctl).contrlRect;
- ensure(RectValid(contrlRect));
- }
-
- /* true if control is visible */
- Boolean CtlVisible(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return((**ctl).contrlVis != 0);
- }
-
- /* return control's hilite */
- Byte CtlHilite(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return((**ctl).contrlHilite);
- }
-
- /* invalidate the control */
- void CtlInval(ControlHandle ctl)
- {
- Rect r;
- GrafPtr port = NULL;
-
- GetPort(&port);
- SetPort(CtlWindow(ctl));
- CtlRect(ctl, &r);
- InvalRect(&r);
- SetPort(port);
- }
-
- /* return next control */
- ControlHandle CtlNext(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return((**ctl).nextControl);
- }
-
- /* return control's value */
- short CtlValue(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return(GetCtlValue(ctl));
- }
-
- /* set control's value */
- void CtlValueSet(ControlHandle ctl, short value)
- {
- require(CtlValid(ctl));
- SetCtlValue(ctl, value);
- }
-
- /* enable or disable the control */
- void CtlEnableSet(ControlHandle ctl, Boolean enable)
- {
- require(CtlValid(ctl));
- HiliteControl(ctl, enable ? CTL_ENABLED : CTL_DISABLED);
- }
-
- /* true if the control is enabled */
- Boolean CtlEnabled(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- return((**ctl).contrlHilite != CTL_DISABLED);
- }
-
- /* get title of control */
- void CtlTitle(ControlHandle ctl, CStr255 title)
- {
- require(CtlValid(ctl));
- require(StrValid(title, -1));
- GetCTitle(ctl, (StringPtr) title);
- p2cstr((StringPtr) title);
- }
-
- /* set title of control */
- void CtlTitleSet(ControlHandle ctl, const CStr255 title)
- {
- Str255 pstr;
-
- require(CtlValid(ctl));
- require(StrValid(title, sizeof(CStr255)));
- SetCTitle(ctl, c2pstrcpy(pstr, title));
- }
-
- /* show or hide the control */
- static void CtlShowHide(ControlHandle ctl, Boolean show)
- {
- require(CtlValid(ctl));
- if (show)
- ShowControl(ctl);
- else
- HideControl(ctl);
- }
-
- /* Define the region containing the default control's outline. The advantage
- of defining a region is that EraseRgn can be used to erase the control's
- outline. The advantage of using EraseRgn is that it will work regardless
- of the background pattern and color of the window. To draw the outline
- a call to PaintRgn suffices. This method contrasts with that recommended
- in IM-I, p407. */
- static void CtlDefaultRgn(ControlHandle ctl, RgnHandle frameRgn)
- {
- Rect frame; /* rectangle to frame */
- PenState pen; /* saved pen */
- GrafPtr port; /* saved port */
-
- require(CtlValid(ctl));
- require(ValidateRgn(frameRgn));
- GetPort(&port);
- SetPort(CtlWindow(ctl));
- GetPenState(&pen);
- PenNormal();
- CtlRect(ctl, &frame);
- check(! thePort->rgnSave);
- OpenRgn();
- InsetRect(&frame, -1, -1);
- FrameRoundRect(&frame, 10, 10);
- InsetRect(&frame, -3, -3);
- FrameRoundRect(&frame, 16, 16);
- CloseRgn(frameRgn);
- SetPenState(&pen);
- SetPort(port);
- }
-
- /* Draw the outline around the default button. The ouline is drawn in
- black if the control is enabled and its window is frontmost, otherwise
- the outline is drawn in gray. If 'pat' isn't NULL the outline is
- drawn in the pattern. */
- void CtlDefaultFrame(ControlHandle ctl, Pattern pat)
- {
- RgnHandle frameRgn = NULL; /* region containing frame */
- GrafPtr port = NULL; /* saved port */
- PenState pen; /* saved pen state */
-
- require(CtlValid(ctl));
-
- /* create region to contain frame */
- frameRgn = BeginRgn();
-
- /* setup port and pen */
- GetPort(&port);
- SetPort(CtlWindow(ctl));
- GetPenState(&pen);
- PenNormal();
-
- /* define region containing frame */
- CtlDefaultRgn(ctl, frameRgn);
-
- /* use gray if control is inactive */
- if (! CtlEnabled(ctl))
- PenPat(gray);
-
- /* draw frame */
- if (pat)
- PenPat(pat);
- PaintRgn(frameRgn);
-
- EndRgn(frameRgn);
- SetPenState(&pen);
- SetPort(port);
- }
-
- /* erase the default button's frame */
- void CtlDefaultErase(ControlHandle ctl)
- {
- RgnHandle frameRgn = NULL;
- GrafPtr port = NULL;
-
- require(CtlValid(ctl));
- GetPort(&port);
- SetPort(CtlWindow(ctl));
- frameRgn = BeginRgn();
- CtlDefaultRgn(ctl, frameRgn);
- EraseRgn(frameRgn);
- InvalRgn(frameRgn);
- EndRgn(frameRgn);
- SetPort(port);
- }
-
- /* flash the button */
- void CtlFlashButton(ControlHandle ctl)
- {
- short hilite;
-
- require(CtlValid(ctl));
-
- if (CtlEnabled(ctl)) {
- hilite = (**ctl).contrlHilite;
- HiliteControl(ctl, inButton);
- TimeDelay(8);
- HiliteControl(ctl, hilite);
- }
- }
-
- /* event handling stuff */
-
- #include "EventLib.h"
-
- /* true if the point is within the control */
- Boolean CtlWithin(ControlHandle ctl, Point where)
- {
- require(CtlValid(ctl));
- return(TestControl(ctl, where) != 0);
- }
-
- short CtlMouseDown(ControlHandle ctl, EventRecord *event)
- {
- Point where;
-
- require(CtlValid(ctl));
- where = event->where;
- GlobalToPort(&where, CtlWindow(ctl));
- return(TrackControl(ctl, where, (ProcPtr) -1));
- }
-
- void CtlInitialize(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- WinRegister(CtlWindow(ctl), ctl, CtlEventTable());
- }
-
- void CtlUninitialize(ControlHandle ctl)
- {
- require(CtlValid(ctl));
- WinUnregister(CtlWindow(ctl), ctl);
- }
-